Scenario based problems
(Style - calculate & count data stored in array)
Objectives : Student should be able to -
Q1. The 1D array StudentName[ ] contains the names of students in a class. The 2D array StudentMark[ ] contains the mark for each subject, for each student. The position of each student’s data in the two arrays is the same, for example, the student in position 10 in StudentName[ ] and StudentMark[ ] is the same.
The variable ClassSize contains the number of students in the class. The variable SubjectNo contains the number of subjects studied. All students study the same number of subjects.
The arrays and variables have already been set up and the data stored.
Students are awarded a grade based on their average mark.
Average mark | Grade awarded |
greater than or equal to 70 | Distinction |
greater than or equal to 55 and less than 70 | Merit |
greater than or equal to 40 and less than 55 | Pass |
less than 40 | Fail |
Write a program that meets the following requirements :
You must use pseudocode code and add comment to explain how your code works.
You do not need to initialise the data in the array.
'// Declare constants for different grade awarded
CONSTANT Distinction ‹— 70
CONSTANT Merit ‹— 55
CONSTANT Pass ‹— 40
'// Initialise variables to count students with different grades
NoDistinction ‹— 0
NoMerit ‹— 0
NoPass ‹— 0
NoFail ‹— 0
'// Outer-loop to repeat for each student of the class (row wise of 1D & 2D array)
FOR Student = 1 TO ClassSize
'// Initialisation of variables to calculate total subject marks of each student
StdTotal ‹— 0
'// Inner-loop to repeat for different subjects (column wise of 2D array)
FOR Subject = 1 TO SubjectNo
'// Calculate total mark of each student
StdTotal ‹— StdTotal + StudentMark[Student, Subject]
NEXT Subject
'// Calculate and round the average mark to its nearest whole number
AverageMark ‹— ROUND(StdTotal / SubjectNo, 0)
'// Check, count and store different grade awarded
IF AverageMark >= Distinction THEN
NoDistinction ‹— NoDistinction + 1
GradeAwarded ‹— “Distinction”
ELSE
IF AverageMark >= Merit THEN
NoMerit ‹— NoMerit + 1
GradeAwarded ‹— “Merit”
ELSE
IF AverageMark >= Pass THEN
NoPass ‹— NoPass + 1
GradeAwarded ‹— “Pass”
ELSE
NoFail ‹— NoFail + 1
GradeAwarded ‹— “Fail”
ENDIF
ENDIF
ENDIF
'// Output each student's details
OUTPUT “Name : ”, StudentName[Std]
OUTPUT “Total mark : ”, StdTotal
OUTPUT “Average mark : ”, AverageMark
OUTPUT “Grade awarded : ”, GradeAwarded
NEXT Student
'// Output the total number of students awarded with different grade
OUTPUT “Number of Distinctions = ”, NoDistinction
OUTPUT “Number of Merits = ”, NoMerit
OUTPUT “Number of Passes = ”, NoPass
OUTPUT “Number of Fails = ”, NoFail
Q2. The one-dimensional (1D) array TeamName[ ] contains the names of teams in a sports league. The two-dimensional (2D) array TeamPoints[ ] contains the points awarded for each match. The position of each team’s data in the two arrays is the same. For example, the team stored at index 10 in TeamName[ ] and TeamPoints[ ] is the same.
The variable LeagueSize contains the number of teams in the league. The variable MatchNo contains the number of matches played. All teams have played the same number of matches.
The arrays and variables have already been set up and the data stored.
Each match can be played at home or away. Points are recorded for the match results of each team with the following values:
Write a program that meets the following requirements:
You must use pseudocode or program code and add comments to explain how your code works.
You do not need to initialise the data in the arrays TeamName[ ] and TeamPoints[ ] or the variables LeaguesSize and MatchNo.
'// Declare constants for different match points
CONSTANT AwayWin ‹— 3
CONSTANT HomeWin ‹— 2
CONSTANT DrawnMatch ‹— 1
CONSTANT LostMatch ‹— 0
'// Initialise variables to find and store league-wise highest and lowest points
HighTeamPoints ‹— 0
LowTeamPoints ‹— 1000
'// Outer-loop to repeat for each team of the league (row-wise in both 1D & 2D array)
FOR Team = 1 TO LeagueSize
'// Initialisation of variables to find and store team-wise data to output
TotalTeamPoints ‹— 0
NoAwayWin ‹— 0
NoHomeWin ‹— 0
NoDrawnMatch ‹— 0
NoLostMatch ‹— 0
'// Inner-loop to repeat for number of match played by each team (column-wise of 2D array)
FOR Match = 1 TO MatchNo
'// Check and count the number of different types of wins
IF TeamPoints[Team, Match] = AwayWin THEN NoAwayWin ‹— NoAwayWin + 1
IF TeamPoints[Team, Match] = HomeWin THEN NoHomeWin ‹— NoHomeWin + 1
IF TeamPoints[Team, Match] = DrawnMatch THEN NoDrawnMatch ‹— NoDrawnMatch + 1
IF TeamPoints[Team, Match] = LostMatch THEN NoLostMatch ‹— NoLostMatch + 1
'// Calculate the total points of each team
TotalTeamPoints ‹— TotalTeamPoints + TeamPoints[Team, Match]
NEXT Match
'// Output details of each team
OUTPUT “Name of the team : ”, TeamName[Team]
OUTPUT “Total points scored : ”, TotalTeamPoints
OUTPUT “Number of away wins : ”, NoAwayWin
OUTPUT “Number of home wins : ”, NoHomeWin
OUTPUT “Number of drawn matches : ”, NoDrawnMatch
OUTPUT “Number of lost matches : ”, NoLostMatch
'// Check and store league-wise highest and lowest points
IF TotalTeamPoints > HighTeamPoints THEN
HighTeamPoints ‹— TotalTeamPoints + 1
'// Store the index of highest scored team
HighTeamID ‹— Team
ENDIF
IF TotalTeamPoints < LowTeamPoints THEN
LowTeamPoints ‹— TotalTeamPoints + 1
'// Store the index of lowest scored team
LowTeamID ‹— Team
ENDIF
NEXT Team
'// Output the name and points of highest and lowest scored team
OUTPUT “Name of the team with highest points is ”, TeamName[HighTeamID]
OUTPUT “Highest scored point is ”, HighTeamPoints
OUTPUT “Name of the team with lowest points is ”, TeamName[LowTeamID]
OUTPUT “Highest scored point is ”, LowTeamPoints
Q3. A hospital stores the name of the patients in a 1D array PatientName[ ] and their temperature in a 2D array PatientTemp [ ] recorded 3-times a day (morning, afternoon and night).
The position of each patient’s data in the two arrays is the same, for example, the patient’s data (name and temperature) in position 5 in PatientName[ ] and PatientTemp[ ] is the same.
The variable NumPatients stores the number of patients. The arrays and variables have already been set up and the data stored.
The temperature status of patient is considered as follows:
Status | Temperature |
Above normal | greater than 100 degree |
Normal | greater than or equal to 97 degree |
Below normal | less than 97 degree |
Write a program for the hospital using pseudocode that meets the following requirements :
You must add comments to explain how your code works. You do not need to initialise the data in the array.
'// Display the available options to select
OUTPUT "1 - Morning"
OUTPUT "2 - Afternoon"
OUTPUT "3 - Evening"
'// Ask to select and input the available option with validation
REPEAT
OUTPUT "Enter the option (between 1 and 3) to show patient details = "
INPUT DayTime
IF (DayTime < 1 OR DayTime > 3) THEN
OUTPUT "Invalid option, re-enter your choice."
ENDIF
UNTIL (DayTime >= 1 AND DayTime <= 3)
'// Declare the constants with lower limit of above and normal temperature
CONSTANT AboveNormal ‹— 100
CONSTANT NormalTemp ‹— 97
'// Initialisation of variables to calculate total and count temperature status
TotalTemp ‹— 0
NoAboveTemp ‹— 0
NoNormalTemp ‹— 0
NoBelowTemp ‹— 0
'// Loop to repeat for each patient
FOR Patient = 1 TO NumPatient
'// Check the temperature status
IF PatientTemp[Patient, DayTime] > AboveNormal THEN
TempStatus ‹— “above normal”
NoAboveTemp ‹— NoAboveTemp + 1
ELSE
IF PatientTemp[Patient, DayTime] >= NormalTemp THEN
TempStatus ‹— “normal”
NoNormalTemp ‹— NoNormalTemp + 1
ELSE
IF PatientTemp[Patient, DayTime] < NormalTemp THEN
TempStatus ‹— “below normal”
NoBelowTemp ‹— NoBelowTemp + 1
ENDIF
ENDIF
ENDIF
'// Calculate the running total of temperature
TotalTemp ‹— TotalTemp + PatientTemp[Patient, DayTime]
'// Output each patient's details
OUTPUT “Name of patient = ”, PatientName[Patient]
OUTPUT “Patient temperature = ”, PatientTemp[Patient, DayTime]
OUTPUT “Patient temperature status is ”, TempStatus
NEXT Patient
'// Calculate average temperature of the patients
AverageTemp ‹— TotalTemp / NumPatients
'// Output details of patients for chosen interval of time
OUTPUT “Number of patients above normal is ”, NoAboveTemp
OUTPUT “Number of patients with normal temperature ”, NoNormalTemp
OUTPUT “Number of patients below normal is ”, NoBelowTemp
OUTPUT “Average temperature of the patients is ”, AverageTemp
Q4. A mobile company has introduced a new feature in their mobile phones where customers can earn points by completing certain tasks. The company has stored the names of all its customers in a 1D array CustomerName[ ]. The 2D array CustomerPoints[ ] contains the points earned by each customer for each task they have completed.
The position of each customer’s data in the two arrays is the same, for example, the customer in position 10 in CustomerName[ ] and CustomerPoints[ ] is the same.
The variable CustomerCount contains the number of customers in the company. The variable TaskCount contains the number of tasks that can be completed to earn points. All the customers have the option to complete the same number of tasks.
The arrays and variables have already been set up and the data stored.
Customers are awarded a reward based on the total number of points they have earned.
Reward | Points earned |
Gold | more than or equal to 1000 points |
Silver | more than or equal to 500 points and less than 1000 points |
Bronze | more than or equal to 100 points and less than 500 points |
Write a program that meets the following requirements :
You must add comments to explain how your code works. You do not need to initialise the data in the array.
'// Declare constants for different rewards
CONSTANT Gold ‹— 1000
CONSTANT Silver ‹— 500
CONSTANT Bronze ‹— 100
'// Initialise variables to count customers with different rewards
NoGold ‹— 0
NoSilver ‹— 0
NoBronze ‹— 0
NoReward ‹— 0
'// Outer-loop to repeat for each customer (row wise of 1D & 2D array)
FOR Customer = 1 TO CustomerCount
'// Initialisation of variables to calculate total points of each customer
CTotal ‹— 0
'// Inner-loop to repeat each task (column wise of 2D array)
FOR Task = 1 TO TaskCount
'// Calculate total points of each customer
CTotal ‹— CTotal + CustomerPoints[Customer, Task]
NEXT Task
'// Calculate and round the average points
AveragePoints ‹— ROUND(CTotal / TaskCount, 0)
'// Check, count and store different rewards
IF AveragePoints >= Gold THEN
NoGold ‹— NoGold + 1
Reward ‹— “Gold”
ENDIF
IF AveragePoints >= Silver AND AveragePoints < Gold THEN
NoSilver ‹— NoSilver + 1
Reward ‹— “Silver”
ENDIF
IF AveragePoints >= Bronze AND AveragePoints < Silver THEN
NoBronze ‹— NoBronze + 1
Reward ‹— “Bronze”
ENDIF
IF AveragePoints < Bronze THEN
NoReward ‹— NoReward + 1
Reward ‹— “no reward”
ENDIF
'// Output each customer's details
OUTPUT “Name of customer : ”, CustomerName[Customer]
OUTPUT “Total points earned : ”, CTotal
OUTPUT “Average points earned : ”, AveragePoints
OUTPUT “Rewarded with : ”, Reward
NEXT Customer
'// Output the total number of customers with different rewards
OUTPUT “Number of customer reward with Gold is ”, NoGold
OUTPUT “Number of customer reward with Silver is ”, NoSilver
OUTPUT “Number of customer reward with Bronze is ”, NoBronze
OUTPUT “Number of customer without any reward is ”, NoReward
* * * * * * * * *
* * * * * *
* * *
*